<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\BaseController;
use app\model\ProductsType;

class Producttype extends BaseController
{
    /**
     * 产品分类
     * @param int $limit
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function index($limit = 10)
    {
        $name = request()->param('name');
        $status = request()->param('status');
        $time = request()->param('time');
        $data = ProductsType::order('sort', 'desc')
            ->when($name, function ($query) use ($name) {
                $query->where('name', 'like', "%$name%");
            })->when($status, function ($query) use ($status) {
                $query->where('status', $status);
            })->when($time, function ($query) use ($time) {
                $query->whereBetween('create_time', $time);
            })->paginate($limit);
        return $this->success($data);
    }

    public function create()
    {
        ProductsType::create(request()->param());
        return $this->success();
    }

    public function update($id)
    {
        ProductsType::where('id',$id)->update(request()->param());
        return $this->success();
    }

    public function delete($id)
    {
        ProductsType::where('id','in',$id)->delete();
        return $this->success();
    }
}
